package org.example.netty.test.nio.netty.chat.bean;

import io.netty.channel.ChannelHandlerContext;
import org.example.netty.test.nio.netty.chat.common.MessageStatus;
import org.example.netty.test.nio.netty.chat.common.MessageType;
import org.example.netty.test.nio.netty.chat.common.SystemId;
import org.example.netty.test.nio.netty.chat.common.SystemMessageType;
import org.example.netty.test.nio.netty.chat.util.NettyChannelUtil;

/**
 * @author zp
 * @version 1.0
 * @description: 系统消息
 * @date 2023/4/4 20:06
 */
public class SystemMessage extends AbsMessageBase{
    private SystemMessageType systemMessageType;

    public SystemMessageType getSystemMessageType() {
        return systemMessageType;
    }

    public void setSystemMessageType(SystemMessageType systemMessageType) {
        this.systemMessageType = systemMessageType;
    }

    @Override
    public int getMessageType() {
        return MessageType.MESSAGE_TYPE_SYSTEM;
    }

    /**
     * @description: 发送正确的消息
     * @author zp
     * @date: 2023/4/6 18:47
     * @param content
     * @return SystemMessage
     */
    public static void sendSuccessMessage(String content, ChannelHandlerContext ctx) throws Exception {
        SystemMessage sendMes = new SystemMessage();
        sendMes.setSender(SystemId.SERVER_ID);
        User userServer = NettyChannelUtil.getUserServer(ctx.channel());
        sendMes.setRecipient(userServer.getUserId());
        sendMes.setSystemMessageType(SystemMessageType.SUCCESS);
        sendMes.setContent(content);
        sendMes.setMessageTypeStatus(MessageStatus.SUCCESS);
        ctx.channel().writeAndFlush(sendMes);
    }

    /**
     * @description: 发送错误消息
     * @author zp
     * @date: 2023/4/7 19:55
     * @param content
     * @return SystemMessage
     */
    public static void sendFailMessage(String content, ChannelHandlerContext ctx) throws Exception {
        SystemMessage sendMes = new SystemMessage();
        sendMes.setSender(SystemId.SERVER_ID);
        User userServer = NettyChannelUtil.getUserServer(ctx.channel());
        if(userServer==null){
            sendMes.setRecipient(SystemId.NULL_ID);
        }else {
            sendMes.setRecipient(userServer.getUserId());
        }
        sendMes.setSystemMessageType(SystemMessageType.FAIL);
        sendMes.setContent(content);
        sendMes.setMessageTypeStatus(MessageStatus.FAIL);
        ctx.channel().writeAndFlush(sendMes);
    }


    /**
     * @description: 组装消息并发送
     * @author zp
     * @date: 2023/4/6 18:47
     * @param systemMessageType
     * @return SystemMessage
     */
    public static void sendSuccessMessage(SystemMessageType systemMessageType, ChannelHandlerContext ctx) throws Exception {
        SystemMessage sendMes = new SystemMessage();
        sendMes.setSender(SystemId.SERVER_ID);
        User userServer = NettyChannelUtil.getUserServer(ctx.channel());
        if(userServer==null){
            sendMes.setRecipient(SystemId.NULL_ID);
        }else {
            sendMes.setRecipient(userServer.getUserId());
        }
        sendMes.setSystemMessageType(systemMessageType);
        sendMes.setContent(systemMessageType.getMessage());
        sendMes.setMessageTypeStatus(MessageStatus.SUCCESS);
        ctx.channel().writeAndFlush(sendMes);
    }

    /**
     * @description: 组装消息并发送
     * @author zp
     * @date: 2023/4/6 18:47
     * @param systemMessageType
     * @return SystemMessage
     */
    public static void sendFailMessage(SystemMessageType systemMessageType, ChannelHandlerContext ctx) {
        SystemMessage sendMes = new SystemMessage();
        sendMes.setSender(SystemId.SERVER_ID);
        User userServer = NettyChannelUtil.getUserServer(ctx.channel());
        if(userServer==null){
            sendMes.setRecipient(SystemId.NULL_ID);
        }else {
            sendMes.setRecipient(userServer.getUserId());
        }
        sendMes.setSystemMessageType(systemMessageType);
        sendMes.setContent(systemMessageType.getMessage());
        sendMes.setMessageTypeStatus(MessageStatus.FAIL);
        ctx.channel().writeAndFlush(sendMes);
    }
}
